#include "playscene.h"
#include <QMenuBar>
#include <QMenu>
#include <QPainter>
#include <QTimer>
#include <QLabel>
#include <QtDebug>
#include <QPropertyAnimation>
#include <QtMultimedia/QSound>
#include "mycoin.h"
#include "dataconfig.h"
#include "mypushbutton.h"

PlayScene::PlayScene(int level,QWidget *parent):QMainWindow(parent),m_nCurrentLevel(level){
    init();
    setConnect();
}

void PlayScene::init(){
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("翻金币");

    QMenuBar* bar = this->menuBar();
    QMenu* startMenu = bar->addMenu("开始");
    m_QuitAction = startMenu->addAction("退出");
    m_CloseBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png",this);
    m_CloseBtn->move(this->width()-m_CloseBtn->width(),this->height()-m_CloseBtn->height());

    QLabel* label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str = QString("Leavel:%1").arg(this->m_nCurrentLevel);
    label->setText(str);
    label->setGeometry(QRect(30,this->height()-50,120,50));

    m_winLabel = new QLabel(this);
    QPixmap pixmap;
    pixmap.load(":/res/LevelCompletedDialogBg.png");
    m_winLabel->setGeometry(0,0,pixmap.width(),pixmap.height());
    m_winLabel->setPixmap(pixmap);
    m_winLabel->move((this->width()-pixmap.width())*0.5,-pixmap.height());

    m_flipSound = new QSound(":/res/ConFlipSound.wav",this);
    m_winSound = new QSound(":/res/LevelWinSound.wav",this);

    m_isWin = false;
    //创建金币背景图片
    for(int i = 0;i <4;i++){
        for(int j = 0;j < 4;j++){
            QLabel* label = new QLabel;
            label->setParent(this);
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode.png"));
            label->move(57 + i * 50,200 + j * 50);
        }
    }

    //载入金币数据
    dataConfig config;
    for(int i = 0;i < 4;i++){
        for(int j = 0;j < 4;j++){
            gameArray[i][j] = config.mData[m_nCurrentLevel][i][j];
        }
    }

    for(int i = 0;i < 4;i++){
        for(int j = 0;j < 4;j++){
            QString img;
            if(gameArray[i][j] == 1){
                img = ":/res/Coin0001.png";
            }else{
                img = ":/res/Coin0008.png";
            }
            MyCoin* coin = new MyCoin(img,this);
            coin->move(59 +i*50,204 + j*50);
            coin->m_posX = i;
            coin->m_posY = j;
            coin->m_flag = gameArray[i][j];
            coinBtn[i][j] = coin;
            connect(coin,&MyCoin::clicked,[=](){
                coin->changeFlag();
                m_flipSound->play();
                gameArray[i][j] ==0 ? 1 : 0;
                QTimer::singleShot(300,this,[=](){
                    if(coin->m_posX + 1 <= 3){
                        coinBtn[coin->m_posX + 1][coin->m_posY]->changeFlag();
                        gameArray[coin->m_posX + 1][coin->m_posY] == 0 ? 1:0;
                    }
                    if(coin->m_posX - 1 >= 0){
                        coinBtn[coin->m_posX - 1][coin->m_posY]->changeFlag();
                        gameArray[coin->m_posX - 1][coin->m_posY] == 0 ? 1:0;
                    }
                    if(coin->m_posY + 1 <= 3){
                        coinBtn[coin->m_posX][coin->m_posY + 1]->changeFlag();
                        gameArray[coin->m_posX][coin->m_posY + 1] == 0 ? 1:0;
                    }
                    if(coin->m_posY - 1 >= 0){
                        coinBtn[coin->m_posX][coin->m_posY - 1]->changeFlag();
                        gameArray[coin->m_posX][coin->m_posY - 1] == 0 ? 1:0;
                    }
                });
                QTimer::singleShot(600,this,[=](){
                    m_isWin = true;
                    for(int i = 0;i < 4;i++){
                        for(int j = 0;j < 4;j++){
                            if(coinBtn[i][j]->m_flag == false){
                                m_isWin = false;
                            }
                        }
                    }
                    if(m_isWin){
                        for(int i = 0;i < 4;i++){
                            for(int j = 0;j < 4;j++){
                                coinBtn[i][j]->m_isWin = true;
                            }
                        }
                        m_winSound->play();
                        qDebug() << "game win";
                        QPropertyAnimation* animation1 = new QPropertyAnimation(m_winLabel,"geometry");
                        animation1->setDuration(1000);
                        animation1->setStartValue(QRect(m_winLabel->x(),m_winLabel->y(),m_winLabel->width(),m_winLabel->height()));
                        animation1->setEndValue(QRect(m_winLabel->x(),m_winLabel->y() + 114,m_winLabel->width(),m_winLabel->height()));
                        animation1->setEasingCurve(QEasingCurve::OutBounce);
                        animation1->start();
                    }
                });
            });
        }
    }
}

void PlayScene::setConnect(){
    connect(m_QuitAction,&QAction::triggered,this,&PlayScene::onAcitionQuitClicked);
    connect(m_CloseBtn,&QPushButton::clicked,this,&PlayScene::onCloseBtnClicked);
}

void PlayScene::onAcitionQuitClicked(){
    this->close();
}

void PlayScene::paintEvent(QPaintEvent*){
    QPainter painter(this);
    QPixmap  pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width() * 0.5,pix.height() * 0.5);
    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);
}

void PlayScene::onCloseBtnClicked(){
    QSound* backSound=new QSound(":/res/BackButtonSound.wav",this);
    backSound->play();
    QTimer::singleShot(500,this,&PlayScene::startReturnToChoose);
}

void PlayScene::startReturnToChoose(){
    this->hide();
    emit playSceneBack();
}
